using MonthlyDB.Entity;
using System;
using System.Data;
using System.IO;
using Wicresoft.Common;
namespace Monthly.Framework.BusinessLogic
{
    public class BLMonthly
    {
        public static void DeleteMonthlys(string[] Ids)
        {
            if (Ids.Length > 0)
            {
                string where = string.Empty;
                for (int i = 0; i < Ids.Length; i++)
                {
                    string id = Ids[i];
                    if (where.Length > 0)
                    {
                        where += " OR ";
                    }
                    where += string.Format(" {0} = '{1}'", "ID", id);
                    BLMonthly.DeleteAttachments(new Guid(id));
                }
                DataAccess.Delete(typeof(MonthlyDB.Entity.Monthly), where);
            }
        }
        public static void DeleteDepts(string[] Ids)
        {
            if (Ids.Length > 0)
            {
                string where = string.Empty;
                for (int i = 0; i < Ids.Length; i++)
                {
                    string id = Ids[i];
                    if (where.Length > 0)
                    {
                        where += " OR ";
                    }
                    where += string.Format(" {0} = '{1}'", "ID", id);
                    BLMonthly.DeleteAttachments(new Guid(id));
                }
                DataAccess.Delete(typeof(MonthlyDB.Entity.Depts), where);
            }
        }

        public static DataTable GetMonthlyList(Guid DeptID, string where, int pageIndex, int pageSize, string order, ref int count)
        {
            return DataAccess.Select(typeof(MonthlyDB.Entity.Monthly), pageIndex, pageSize, "DeptID='" + DeptID + "' and " + where, order, ref count);
        }
        public static DataTable GetMonthlyList( string where, int pageIndex, int pageSize, string order, ref int count)
        {
            return DataAccess.Select(typeof(MonthlyDB.Entity.Monthly), pageIndex, pageSize,   where, order, ref count);
        }
        public static DataTable GetDeptList(string where, int pageIndex, int pageSize, string order, ref int count)
        {
            return DataAccess.Select(typeof(MonthlyDB.Entity.Depts), pageIndex, pageSize, where, order, ref count);
        }
        public static DataTable getAttachmentsByMonthlyID(Guid MonthlyID)
        {
            return DataAccess.Select(typeof(Attachment), string.Format("{0} = '{1}'", "MonthlyID", MonthlyID));
        }
        public static void DeleteAttachments(DataTable dtAttachment)
        {
            foreach (DataRow dr in dtAttachment.Rows)
            {
                DataAccess.Delete(typeof(Attachment), string.Format("{0} = '{1}'", "ID", dr["ID"]));
                File.Delete(dr["PhysicalPath"].ToString());
            }
        }
        public static void DeleteAttachments(Guid MonthlyID)
        {
            DataTable dt = BLMonthly.getAttachmentsByMonthlyID(MonthlyID);
            BLMonthly.DeleteAttachments(dt);
        }
    }
}
